err0 = sum( sum( (ts - A * S).^2 ) ) ;
Ao = A ;
S =S .*( ( A'* ts ) ./ ( A' * ( A * S ) ) ) ;
A = A .* ( ( ts * S' ) ./ ( ( A * S ) *S' ) ) ;
err = sum( sum( (ts - A * S).^2 ) ) ;
stop = ( err0 - err ).^2 < 10 ;
iter = 1 ;
while ~( stop || iter > 1000 )
err0 = err ;
S =S .*( ( A'* ts ) ./ ( A' * ( A * S ) ) ) ;
A = A .* ( ( ts * S' ) ./ ( ( A * S ) *S' ) ) ;
err = sum( sum( (ts - A * S).^2 ) ) ;
stop = ( err0 - err ).^2 < 10 ;
iter = iter + 1 ;
end